package getDate

import (
	"gin/util"
	"strconv"
	"strings"
	"time"
)

// GetMonthLastDay 找到选择的那个月最后一天的日期
func GetMonthLastDay(yearMonth string) string {
	arr := strings.Split(yearMonth, "-")
	now := time.Now()
	year, _ := strconv.Atoi(arr[0])
	month, _ := strconv.Atoi(arr[1])
	years := year - now.Year()
	months := years*12 + month - int(now.Month()) + 1
	_, _, day := now.Date()
	//获得时间，并且转成字符串
	lastTime := now.AddDate(0, months, -day).String()
	return util.Substr(lastTime, 0, 10)
}

// DateFormat 时间戳转年月日时分秒
func DateFormat(timestamp int) string {
	tm := time.Unix(int64(timestamp), 0)
	return tm.Format("2006-01-02 15:04:00")
}

// DateFormatYmd 时间戳转年月日
func DateFormatYmd(timestamp int) string {
	tm := time.Unix(int64(timestamp), 0)
	return tm.Format("2006-01-02")
}

// DateNowFormatStr 时间戳转年月日
func DateNowFormatStr(timestamp int) string {
	tm := time.Now()
	return tm.Format("2006-01-02 15:04:00")
}

//DateUnix 时间戳
func DateUnix() int {
	t := time.Now().Local().Unix() * 1000
	return int(t)
}

// GetTime 时间转时间戳- 2020-12-3012:00: 00
func GetTime(v string) int {
	tm2, _ := time.Parse("2006-01-0215:04:05", v)

	return int(tm2.Unix() * 1000)
}

// DateNowFormat 获取年月日时分秒(time类型)
func DateNowFormat() time.Time {
	tm := time.Now()
	return tm
}
